package com.prestamomo.vil.ppa.pmvilpage;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.prestamomo.vil.ppa.BuildConfig;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilapi.PmvilSerConfig;
import com.prestamomo.vil.ppa.pmvilapi.PmvilUserRz;
import com.prestamomo.vil.ppa.pmvilapi.PmvilUsrOcr;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilPermissMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.pmcamera.PmvilParamBean;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilAtyRoot;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import jsc.kit.wheel.dialog.DateTimeWheelDialog;

public class PmvilPlaceVy extends PmvilAtyRoot {

    private boolean pmvilIsface;
    private boolean pmvilIschange;
    private ImageView placePmvilIvback;
    private LinearLayout placePmvilLlhead;
    private RelativeLayout placePmvilRlpsrl;
    private RelativeLayout placePmvilRlSsrl;
    private ImageView placePmvilIvrl;
    private ImageView placePmvilIvCleanrl;
    private RelativeLayout placePmvilRlpszm;
    private RelativeLayout placePmvilRlSszm;
    private ImageView placePmvilIvzm;
    private ImageView placePmvilIvCleanzm;
    private RelativeLayout placePmvilRlpsfm;
    private RelativeLayout placePmvilRlSsfm;
    private ImageView placePmvilIvfm;
    private ImageView placePmvilIvCleanfm;
    private EditText placePmvilEtname;
    private EditText placePmvilEtnum;
    private TextView placePmvilTvtj;
    private TextView placePmvilTvsher;
    private LinearLayout placePmvilLlsher;
    private TextView placePmvilTvxb;
    private LinearLayout placePmvilLlxb;
    private String pmvilRlResult;
    private String pmvilZmresult;
    private String pmvilFmresult;
    private String mGreanType;
    private PopupWindow mPopupWindow;

    @Override
    protected int getPmvilLayoutId() {
        return R.layout.place_pmvil_ly;
    }

    @Override
    protected void showPmvilUi() {
        pmvilIsface = getIntent().getBooleanExtra("pmvil_isface", false);
        pmvilIschange = true;//getIntent().getBooleanExtra("pmvil_ischange", false);

        placePmvilIvback = findViewById(R.id.place_pmvil_iv_back);
        placePmvilLlhead = findViewById(R.id.place_pmvil_ll_head);
        placePmvilRlpsrl = findViewById(R.id.place_pmvil_rl_psrl);
        placePmvilRlSsrl = findViewById(R.id.place_pmvil_rl_ssrl);
        placePmvilIvrl = findViewById(R.id.place_pmvil_iv_rl);
        placePmvilIvCleanrl = findViewById(R.id.place_pmvil_iv_clean_rl);

        placePmvilRlpszm = findViewById(R.id.place_pmvil_rl_pszm);
        placePmvilRlSszm = findViewById(R.id.place_pmvil_rl_sszm);
        placePmvilIvzm = findViewById(R.id.place_pmvil_iv_zm);
        placePmvilIvCleanzm = findViewById(R.id.place_pmvil_iv_clean_zm);

        placePmvilRlpsfm = findViewById(R.id.place_pmvil_rl_psfm);
        placePmvilRlSsfm = findViewById(R.id.place_pmvil_rl_ssfm);
        placePmvilIvfm = findViewById(R.id.place_pmvil_iv_fm);
        placePmvilIvCleanfm = findViewById(R.id.place_pmvil_iv_clean_fm);

        placePmvilEtname = findViewById(R.id.place_pmvil_et_name);
        placePmvilEtnum = findViewById(R.id.place_pmvil_et_num);
        placePmvilTvtj = findViewById(R.id.place_pmvil_tv_tj);
        placePmvilTvsher = findViewById(R.id.place_pmvil_tv_shr);
        placePmvilTvxb = findViewById(R.id.place_pmvil_tv_xb);
        placePmvilLlsher = findViewById(R.id.place_pmvil_ll_shr);
        placePmvilLlxb = findViewById(R.id.place_pmvil_ll_xb);

        if (pmvilIsface) {
            placePmvilLlhead.setVisibility(View.VISIBLE);
            sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dfwejyrgee);
            sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dfwejyrgee_0);
        } else {
            placePmvilLlhead.setVisibility(View.GONE);
        }
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ltvdtuff);
        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_ltvdtuff_0);

        placePmvilEtname.addTextChangedListener(getPmvilWatcher());
        placePmvilEtnum.addTextChangedListener(getPmvilWatcher());
    }

    @NonNull
    private TextWatcher getPmvilWatcher() {
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                placePmvilEtnum.setTextColor(Color.parseColor("#333333"));
                placePmvilEtname.setTextColor(Color.parseColor("#333333"));
            }
        };
    }
    DateTimeWheelDialog plsdialog;
    @Override
    protected void clickePmvil() {
        placePmvilIvback.setOnClickListener(v -> showPmvilWaliuDlg());

        placePmvilRlpsrl.setOnClickListener(v -> {
            if (PmvilPermissMd.checkPmlCameraPermiss(pmvilAy)) {
                showPsPmvilDlg();
            } else {
                PmvilPermissMd.getPmvilPermiss(pmvilAy, PmvilPermissMd.pmvilPermissCamera);
            }
        });
        placePmvilRlpszm.setOnClickListener(v -> {
            if (PmvilPermissMd.checkPmlCameraPermiss(pmvilAy)) {
                showPsPmvilOCrDlg();
            } else {
                PmvilPermissMd.getPmvilPermiss(pmvilAy, PmvilPermissMd.pmvilPermissCamera);
            }

        });
        placePmvilLlsher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(plsdialog==null){
                    plsdialog = createDialog(3);
                }else{
                    plsdialog.show();
                }
            }
        });
        placePmvilLlxb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               showPopGreand(placePmvilTvxb);
            }
        });
        placePmvilRlpsfm.setOnClickListener(v -> {
            if (PmvilPermissMd.checkPmlCameraPermiss(pmvilAy)) {
                pmvilPsocrted(10023);
            } else {
                PmvilPermissMd.getPmvilPermiss(pmvilAy, PmvilPermissMd.pmvilPermissCamera);
            }
        });

        placePmvilIvCleanrl.setOnClickListener(v -> cleanPmvilImgeShow());
        placePmvilIvCleanzm.setOnClickListener(v -> cleanPmvilImgeShow());
        placePmvilIvCleanfm.setOnClickListener(v -> cleanPmvilImgeShow());

        placePmvilTvtj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pmvilIsface){
                    if(!TextUtils.isEmpty(pmvilRlResult)){
                        postPmvilFace();
                        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_nxregdgrgr);
                        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_nxregdgrgr_0);
                    }else{
                        PmvilTosMd.showPmvaMsg("資訊不完整");
                    }
                }else{
                    if(!TextUtils.isEmpty(placePmvilTvxb.getText().toString())&&!TextUtils.isEmpty(placePmvilTvsher.getText().toString())&&!TextUtils.isEmpty(placePmvilEtname.getText().toString())&&!TextUtils.isEmpty(placePmvilEtnum.getText().toString())){
                        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_nxregdgrgr);
                        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_nxregdgrgr_0);
                        postOcrPmvilInfo();
                    }else{
                        PmvilTosMd.showPmvaMsg("資訊不完整");
                    }
                }
            }
        });

    }

    private void showPopGreand(TextView placePmvilTvxb) {
        View pmView = getLayoutInflater().inflate(R.layout.poplies,null);
        TextView tvNan = pmView.findViewById(R.id.place_pmvil_tv_xbnan);
        TextView tvNv = pmView.findViewById(R.id.place_pmvil_tv_xbnv);
        if(mPopupWindow==null){
            mPopupWindow = new PopupWindow(pmView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        mPopupWindow.setOutsideTouchable(true);//设置点击外部区域可以取消popupWindow
        tvNan.setOnClickListener(view -> {
            placePmvilTvxb.setText("男");
            mGreanType = "1";
            mPopupWindow.dismiss();
        });
        tvNv.setOnClickListener(view -> {
            placePmvilTvxb.setText("女");
            mGreanType = "2";
            mPopupWindow.dismiss();
        });
        if(mPopupWindow!=null&&!mPopupWindow.isShowing()){
            mPopupWindow.showAsDropDown(placePmvilTvxb);
        }
    }

    private DateTimeWheelDialog createDialog(int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)-90);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        Date startDate = calendar.getTime();
        calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)+1);
        Date endDate = calendar.getTime();

        DateTimeWheelDialog dialog = new DateTimeWheelDialog(this);
        dialog.show();
        dialog.setTitle("選擇生日");
        int config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR_MINUTE;
        switch (type) {
            case 1:
                config = DateTimeWheelDialog.SHOW_YEAR;
                break;
            case 2:
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH;
                break;
            case 3:
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY;
                break;
            case 4:
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR;
                break;
            case 5:
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR_MINUTE;
                break;
        }
        dialog.configShowUI(config);
        dialog.setCancelButton("取消", null);
        dialog.setOKButton("确定", new DateTimeWheelDialog.OnClickCallBack() {
            @Override
            public boolean callBack(View v, @NonNull Date selectedDate) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                placePmvilTvsher.setText(simpleDateFormat.format(selectedDate));
                return false;
            }
        });
        dialog.setDateArea(startDate, endDate, true);
        dialog.updateSelectedDate(new Date());
        return dialog;
    }
    private void getPmvilQueryUser() {
        showPmvilLoading();
        PmvilHttpRequest.getPmvilQueryUser(this,  new PmvilCallBack<PmvilUserRz>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilUserRz pmvilContent) {
                if (pmvilContent.getLivenessStatus()&&pmvilContent.getHoldIdReslut().equals("0")) {
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jrthsfgerg_t);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jrthsfgerg_t_0);
                    Intent pmvilIntent = new Intent(pmvilAy, PmvilBlockVy.class);
                    startActivity(pmvilIntent);
                    finish();
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilUserRz pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilUserRz pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsrcsfwdsg_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsrcsfwdsg_e_0);

            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }
    private void postOcrPmvilInfo() {
        showPmvilLoading();
        PmvilHttpRequest.postInfoPmvilOcr(this, placePmvilEtname.getText().toString(),
                placePmvilEtnum.getText().toString(),
                placePmvilTvsher.getText().toString(),mGreanType,new PmvilCallBack<Boolean>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jrthsfgerg_t);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_jrthsfgerg_t_0);
                getPmvilQueryUser();
                //getCheckPmvilPage(true);
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsrcsfwdsg_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsrcsfwdsg_e_0);
                if(pmvilIschange){
                    placePmvilEtname.setTextColor(Color.RED);
                    placePmvilEtnum.setTextColor(Color.RED);
                }
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsrcsfwdsg_e);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_bsrcsfwdsg_e_0);
            }
        });
    }


    private void cleanPmvilImgeShow() {
        if(pmvilIsface){
            placePmvilRlSsrl.setVisibility(View.GONE);
            pmvilRlResult = "";
            placePmvilRlpsrl.setEnabled(true);
        }
        placePmvilRlSszm.setVisibility(View.GONE);
        placePmvilRlSsfm.setVisibility(View.GONE);
        placePmvilRlpszm.setEnabled(true);
        placePmvilRlpsfm.setEnabled(true);
        pmvilZmresult = "";
        pmvilFmresult = "";
        placePmvilEtname.setText("");
        placePmvilEtnum.setText("");
        placePmvilEtname.setEnabled(true);
        placePmvilEtnum.setEnabled(true);
    }

    @Override
    protected void loadPmvilData() {
        //getOcrPmvilCofig(false);
    }



    boolean pmvilIsAllPermiss = false;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200) {
            for (String permission : permissions) {
                pmvilIsAllPermiss = ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
                if (!pmvilIsAllPermiss) {
                    //勾选不在询问
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                        pmvilIsAllPermiss = false;
                        showPmvilPermisDlg(true, R.layout.permis_agen_pmvil_dlg);
                        return;
                    }
                }
            }
            if (!pmvilIsAllPermiss) {
                showPmvilPermisDlg(true, R.layout.permis_agen_pmvil_dlg);
            }
        }
    }

    private void showPsPmvilDlg() {
        AlertDialog.Builder placeRlPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilRlView = this.getLayoutInflater().inflate(R.layout.place_rl_pmvil_dlg, null);
        placeRlPmvilBlder.setView(pmvilRlView);
        placeRlPmvilBlder.setCancelable(false);
        AlertDialog pmvilPermisDlg = placeRlPmvilBlder.create();
        pmvilPermisDlg.show();
        ImageView placerlPmvileImClose = pmvilRlView.findViewById(R.id.placerl_pmvildlg_close);
        TextView placerlPmvilTvIno = pmvilRlView.findViewById(R.id.placerl_pmvildlg_tv_yave);
        placerlPmvileImClose.setOnClickListener(v -> pmvilPermisDlg.dismiss());
        placerlPmvilTvIno.setOnClickListener(v -> {
            pmvilPermisDlg.dismiss();
            Intent intent = new Intent(this, PmvilCameraVy.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PmvilParamBean paramBean = new PmvilParamBean();
            paramBean.setPmvilLandscape(false);
            paramBean.setPmvilEnableTorch(false);
            paramBean.setPmvilCamerType(0);
            paramBean.setPmvilType(2);
            intent.putExtra("pmvil_options", JSONObject.toJSONString(paramBean));
            startActivityForResult(intent, 10021);
        });
    }

    private void showPsPmvilOCrDlg() {
        AlertDialog.Builder placeOcrPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilOcrView = this.getLayoutInflater().inflate(R.layout.ocr_rl_pmvil_dlg, null);
        placeOcrPmvilBlder.setView(pmvilOcrView);
        placeOcrPmvilBlder.setCancelable(false);
        AlertDialog pmvilOcrDlg = placeOcrPmvilBlder.create();
        pmvilOcrDlg.show();
        ImageView placeocrPmvileImClose = pmvilOcrView.findViewById(R.id.ocr_pmvildlg_close);
        TextView placeocrPmvilTvIno = pmvilOcrView.findViewById(R.id.ocr_pmvildlg_tv_yave);
        placeocrPmvileImClose.setOnClickListener(v -> pmvilOcrDlg.dismiss());
        placeocrPmvilTvIno.setOnClickListener(v -> {
            pmvilOcrDlg.dismiss();
            sendAdjPmvilEvent(PmvilAjustinfse.pmvil_sfewhbghdcbn);
            sendAdjPmvilEvent(PmvilAjustinfse.pmvil_sfewhbghdcbn_0);
            pmvilPsocrted(10022);
        });
    }

    private  void pmvilPsocrted(int pmvilCodes) {
        Intent intent = new Intent(this, PmvilCameraVy.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PmvilParamBean paramBean = new PmvilParamBean();
        paramBean.setPmvilLandscape(true);
        paramBean.setPmvilEnableTorch(false);
        paramBean.setPmvilCamerType(1);
        paramBean.setPmvilType(2);
        intent.putExtra("pmvil_options", JSONObject.toJSONString(paramBean));
        startActivityForResult(intent, pmvilCodes);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==1000 && requestCode==10021){
            pmvilRlResult = data.getStringExtra("pmvil_result");
            placePmvilRlSsrl.setVisibility(View.VISIBLE);
            placePmvilRlpsrl.setEnabled(false);
            Glide.with(this).load(pmvilRlResult).into(placePmvilIvrl);
        }else if(resultCode==1000 && requestCode==10022){
            pmvilZmresult = data.getStringExtra("pmvil_result");
            placePmvilRlSszm.setVisibility(View.VISIBLE);
            placePmvilRlpszm.setEnabled(false);
            Glide.with(this).load(pmvilZmresult).into(placePmvilIvzm);
            if(!TextUtils.isEmpty(pmvilFmresult)){
                postPmvilOcr();
            }
        }else if(resultCode==1000 && requestCode==10023){
            pmvilFmresult = data.getStringExtra("pmvil_result");
            placePmvilRlSsfm.setVisibility(View.VISIBLE);
            placePmvilRlpsfm.setEnabled(false);
            Glide.with(this).load(pmvilFmresult).into(placePmvilIvfm);
            if(!TextUtils.isEmpty(pmvilZmresult)){
                postPmvilOcr();
            }
        }
    }

    private void postPmvilFace() {
        Bitmap pmvilFacebitmap = BitmapFactory.decodeFile(pmvilRlResult);
        String facePmvilStr = bitmapPmvilToBase64(pmvilFacebitmap);
        if(!facePmvilStr.startsWith("data:image/jpeg;base64,")){
            facePmvilStr =  "data:image/jpeg;base64,"+facePmvilStr;
        }
        PmvilHttpRequest.postFacePmvilUser(this, facePmvilStr, new PmvilCallBack<Boolean>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, Boolean pmvilContent) {
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fgdgregregr);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fgdgregregr_0);
                postOcrPmvilInfo();
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, Boolean pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, Boolean pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dfwefsew);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dfwefsew_0);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dfwefsew);
                sendAdjPmvilEvent(PmvilAjustinfse.pmvil_dfwefsew_0);
            }
        });
    }

    private void postPmvilOcr() {
            showPmvilLoading();
            Bitmap ocrZmPmvilBitmap = BitmapFactory.decodeFile(pmvilZmresult);
            String ocrZmPmvilStr = bitmapPmvilToBase64(ocrZmPmvilBitmap);
            if(!ocrZmPmvilStr.startsWith("data:image/jpeg;base64,")){
                ocrZmPmvilStr =  "data:image/jpeg;base64,"+ocrZmPmvilStr;
            }
            Bitmap ocrFmPmvilBitmap = BitmapFactory.decodeFile(pmvilFmresult);
            String ocrFmPmvilStr = bitmapPmvilToBase64(ocrFmPmvilBitmap);
            if(!ocrFmPmvilStr.startsWith("data:image/jpeg;base64,")){
                ocrFmPmvilStr =  "data:image/jpeg;base64,"+ocrFmPmvilStr;
            }
            PmvilHttpRequest.postUserPmvilOcr(this, ocrZmPmvilStr, ocrFmPmvilStr, new PmvilCallBack<PmvilUsrOcr>() {
                @Override
                public void onPmvilSucc(String pmvilMethod, PmvilUsrOcr pmvilContent) {
                    hindPmvilLoading();
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fewherdssghg_t);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fewherdssghg_t_0);
                    placePmvilEtname.setEnabled(pmvilIschange);
                    placePmvilEtnum.setEnabled(pmvilIschange);
//                    placePmvilEtname.setText(pmvilContent.getPmvilname());
//                    placePmvilEtnum.setText(pmvilContent.getPmvilidNo());
                }

                @Override
                public void onPmvilLogOn(String pmvilMod, PmvilUsrOcr pmvilCtent) {
                    outpmvilLongin();
                }

                @Override
                public void onPmvilOther(String pmvilMhod, PmvilUsrOcr pmvilCnt, String pmvilCode, String pmvilMsg) {
                    tostPmvilErrMsg(pmvilMsg);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_cbewsewfga_e);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_cbewsewfga_e_0);
                }

                @Override
                public void onPmvilFailure(String pmvilMod, String pmvilError) {
                    tostPmvilErrMsg(pmvilError);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_cbewsewfga_e);
                    sendAdjPmvilEvent(PmvilAjustinfse.pmvil_cbewsewfga_e_0);
                }
            });
    }

    @Override
    public void onBackPressed() {
        showPmvilWaliuDlg();
    }

    /**
     * bitmap to base64
     * @param BitPmvilmap
     * @return
     */
    public static String bitmapPmvilToBase64(Bitmap BitPmvilmap) {
        String pmvilResultStr = null;
        ByteArrayOutputStream pmvilBytarStrs = null;
        Map<String,Object> pmvilBitMasp = new HashMap<>();
        pmvilBitMasp.put("pmvilIsCleatype",0);
        try {
            if (BitPmvilmap != null) {
                pmvilBitMasp.put("pmvilIsCleatype",1);
                pmvilBytarStrs = new ByteArrayOutputStream();
                byte[] BitmapPmvilBytes = getPmvilBitBytes(BitPmvilmap, pmvilBytarStrs);
                pmvilResultStr = Base64.encodeToString(BitmapPmvilBytes, Base64.DEFAULT);
            }else{
                pmvilBitMasp.put("pmvilIsCleatype",2);
            }
        } catch (IOException e) {
            e.printStackTrace();
            pmvilBitMasp.put("pmvilIsCleatype",3);
        } finally {
            if (pmvilBytarStrs != null) {
                try {
                    pmvilBytarStrs.flush();
                    pmvilBytarStrs.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(pmvilBitMasp.size()!=0){
                pmvilBitMasp.clear();
            }else {
                pmvilBitMasp.put("pmvilIsCleatype",0);
            }
        }
        return pmvilResultStr;
    }

    @NonNull
    private static byte[] getPmvilBitBytes(Bitmap BitPmvilmap, ByteArrayOutputStream pmvilBytarStrs) throws IOException {
        BitPmvilmap.compress(Bitmap.CompressFormat.JPEG, 80, pmvilBytarStrs);
        pmvilBytarStrs.flush();
        pmvilBytarStrs.close();
        byte[] BitmapPmvilBytes = pmvilBytarStrs.toByteArray();
        return BitmapPmvilBytes;
    }

}
